import mongoose from "mongoose"

const orderSchema = mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId, // 其他模型的id
    required: true,
    ref: 'User', // 关联用户
  },
  productsItems: [{
    name: {
      type: String,
      required: true
    },
    qty: {
      type: Number,
      required: true
    },
    image: {
      type: String,
      required: true
    },
    price: {
      type: Number,
      required: true
    },
    productId: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: "Product"
    },
  }],
  shippingAddress: {
    address: {
      type: String,
      required: true
    },
    city: {
      type: String,
      required: true
    },
    postalCode: {
      type: String,
      required: true
    },
    province: {
      type: String,
      required: true
    },
  },
  paymentMethod: {
    type: String,
    required: true,
  },
  isPaid: { // 是否支付
    type: Boolean,
    required: true,
    default: false
  },
  paidAt: { //支付日期
    type: Date,
  },
  isDelivered: { // 是否发货
    type: Boolean,
    required: true,
    default: false
  },
  deliveredAt: { // 发货时间
    type: Date
  },
  isReceived: { // 是否已收到
    type: Boolean,
    required: true,
    default: false
  },
  receivedAt: { // 收货时间
    type: Date
  },
  productsPrice: { // 商品总价
    type: Number,
    required: true,
  },
  shippingPrice: {
    type: Number,
    required: true,
  },
  totalPrice:{
    type: Number,
    required: true,
  },
}, {
  timestamps: true, // 更新时更新时间
})

const Order = mongoose.model("Order", orderSchema)


export default Order